// enterpriseMine/post/addPost/addPost.js
import {
    ajaxPost,
    showToast
} from "../../../utils/util";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: "",
        nameVal: "", // 职位名称
        region: ["北京市", "北京市", "东城区"], // 工作地点

        educationArr: [], // 学历列表
        educationIndex: 0, // 学历选择了第几个

        natureArr: [], // 工作性质列表
        natureIndex: 0, // 工作性质选择了第几个

        newWorkTypeArrIndex: [0, 0],
        newWorkTypeArr: [], //展示内容
        workVal: [], //展示内容

        moneyIndex: [0, 0], //薪资范围 key
        moneyArr: [], //薪资范围
        moneyVal: [], //薪资范围展示

        majorIndex: [0, 0], //专业需求 key
        majorArrAll: [], //专业需求 总共
        majorArr: [], //专业需求 展示
        majorList: [] //专业需求 展示
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.type == "add") {
            wx.setNavigationBarTitle({
                title: "发布岗位"
            });
        } else {
            wx.setNavigationBarTitle({
                title: "编辑岗位"
            });

        }
        let data = {
            token: wx.getStorageSync("token")
        };
        ajaxPost("/company/positionConfig", data, res => {
            let {
                position_salary,
                position_nature,
                position_education,
                position_type,
                position_major,
                position_salary_top
            } = res.data.config_list;
            if (res.code == 0) {
                /**
                 * 职位类型
                 */
                let newWorkOneArr = [];
                for (let i in position_type) {
                    newWorkOneArr.push(position_type[i].name);
                }
                let newWorkTwoArr = [];
                let workTwoArr = position_type[0].data;
                for (let i in workTwoArr) {
                    newWorkTwoArr.push(workTwoArr[i].name);
                }
                let newWorkTypeArr = [newWorkOneArr, newWorkTwoArr];
                /**
                 * 薪酬范围
                 */
                let newMoneyArr = [position_salary, position_salary_top];
                /**
                 * 专业需求
                 */
                let newMajorOneArr = [];
                for (let i in position_major) {
                    newMajorOneArr.push(position_major[i].name);
                }
                let newMajorTwoArr = [];
                let MajorTwoArr = position_major[0].data;
                for (let i in MajorTwoArr) {
                    newMajorTwoArr.push(MajorTwoArr[i].name);
                }
                let newMajorArr = [newMajorOneArr, newMajorTwoArr];
                this.setData({
                    workTypeArr: position_type,
                    newWorkTypeArr: newWorkTypeArr,
                    moneyArr: newMoneyArr, // 薪资范围
                    majorArr: newMajorArr, // 专业需求展示列表
                    majorArrAll: position_major, // 专业需求总列表
                    educationArr: position_education,
                    natureArr: position_nature,
                    type: options.type
                });
                if (options.type == "set") {
                    this.getWrokInfo(options.Id)
                }
            }
        });
    },
    /**
     *  获取岗位详情
     * @param {*} e 
     */
    getWrokInfo(id) {
        let data = {
            token: wx.getStorageSync("token"),
            position_id: id,
        };
        ajaxPost("/company/wxpositioninfo", data, res => {
            if (res.code == 0) {
                let addArr = []
                addArr.push(res.data.province);
                addArr.push(res.data.city);
                addArr.push(res.data.district);
                console.log(this.data.natureArr.indexOf(res.data.nature));
                this.setData({
                    id: id,
                    nameVal: res.data.name,
                    workVal: res.data.type,
                    moneyVal: res.data.salary,
                    natureIndex: this.data.natureArr.indexOf(res.data.nature),
                    educationIndex: this.data.educationArr.indexOf(res.data.education),
                    moneyVal: res.data.salary,
                    region: addArr,
                    majorList: res.data.major,
                    textareaVal: res.data.description,
                })
            }
        });
    },
    /**
     *  职位类型二级分类选择监听事件
     * @param {*} 穿参
     */
    bindMultiPickerColumnChange(e) {
        if (e.detail.column == 0) {
            let workTypeArr = this.data.workTypeArr;
            let newTwoArr = [];
            let arrTwo = workTypeArr[e.detail.value].data;
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name);
            }
            let Arr = [this.data.newWorkTypeArr[0], newTwoArr];
            let newIndustryIndex = [e.detail.value, 0];
            this.setData({
                newWorkTypeArr: Arr,
                newIndustryIndex: newIndustryIndex
            });
        }
    },
    /**
     * 职位类型确认
     */
    changeWork(e) {
        let index = e.detail.value;
        let newWorkArr = [];
        newWorkArr.push(this.data.workTypeArr[index[0]].name);
        newWorkArr.push(this.data.workTypeArr[index[0]].data[index[1]].name);
        this.setData({
            workVal: newWorkArr
        });
    },
    /**
     *  薪资范围二级分类选择监听事件
     * @param {*} 穿参
     */
    moneyPickerChange(e) {
        let moneyIndex = this.data.moneyIndex;
        if (e.detail.column == 0) {
            let newMoneyIndex = [e.detail.value, moneyIndex[1]];
            this.setData({
                moneyIndex: newMoneyIndex
            });
        }
        if (e.detail.column == 1) {
            let newMoneyIndex = [moneyIndex[0], e.detail.value];
            this.setData({
                moneyIndex: newMoneyIndex
            });
        }
    },
    /**
     * 薪资范围确认
     */
    changeMoney(e) {
        let index = e.detail.value;
        let newWorkArr = [];
        let moneyArr = this.data.moneyArr;
        newWorkArr.push(moneyArr[0][index[0]]);
        newWorkArr.push(moneyArr[1][index[1]]);
        this.setData({
            moneyVal: newWorkArr
        });
    },
    /**
     * 学历选择
     * @param {*} e
     */
    ChangeEducation(e) {
        this.setData({
            educationIndex: e.detail.value
        });
    },
    /**
     * 工作性质选择
     * @param {*} e
     */
    changeNature(e) {
        this.setData({
            natureIndex: e.detail.value
        });
    },
    /**
     * 工作地点选择
     * @param {*} e
     */
    bindRegionChange(e) {
        this.setData({
            region: e.detail.value
        });
    },
    /**
     * input 输入保存
     */
    bindKeyInput(e) {
        let type = e.target.dataset.type;
        console.log(e);
        this.setData({
            [type]: e.detail.value
        });
    },

    /**
     *  专业需求二级分类选择监听事件
     * @param {*} 穿参
     */
    mahirChange(e) {
        console.log(e);
        if (e.detail.column == 0) {
            let majorArrAll = this.data.majorArrAll;
            let arrTwo = majorArrAll[e.detail.value].data;

            let newTwoArr = [];
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name);
            }
            let Arr = [this.data.majorArr[0], newTwoArr];
            let newMajorIndex = [e.detail.value, 0];
            this.setData({
                majorArr: Arr,
                majorIndex: newMajorIndex
            });
        }
    },
    /**
     * 专业需求确认
     */
    changeMajor(e) {
        let majorList = this.data.majorList;
        if (majorList.length >= 3) {
            showToast("text", "最多三条");
            return;
        }
        let index = e.detail.value;
        let newWorkList = [];
        newWorkList.push(this.data.majorArrAll[index[0]].name);
        newWorkList.push(this.data.majorArrAll[index[0]].data[index[1]].name);
        majorList.push(newWorkList);
        this.setData({
            majorList: majorList
        });
    },
    /**
     * 专业需求删除
     */
    majorDel(e) {
        let index = e.target.dataset.index;
        let majorList = this.data.majorList;
        majorList.splice(index, 1);
        this.setData({
            majorList: majorList
        });
    },
    /**
     * 发布岗位
     */
    addData() {
        let {
            nameVal, //职业名字
            workVal, //职位类型
            region, //工作地点
            educationArr, //最低学历列表
            educationIndex, //最低学历key
            natureArr, //工作性质列表
            natureIndex, //工作性质key
            moneyVal, //薪资范围
            majorList, //专业需求列表
            textareaVal //职位描述
        } = this.data;

        if (!nameVal) {
            showToast("text", "请输入职业名字");
            return;
        }
        if (workVal.length == 0) {
            showToast("text", "请选择职位类型");
            return;
        }
        if (moneyVal.length == 0) {
            showToast("text", "请选择薪资范围");
            return;
        }
        if (!textareaVal) {
            showToast("text", "请职位描述");
            return;
        }
        let data = {
            token: wx.getStorageSync("token"),
            name: nameVal,
            type: workVal,
            salary: moneyVal,
            nature: natureArr[natureIndex],

            education: educationArr[educationIndex],

            province: region[0],
            city: region[1],
            district: region[2],
            major: majorList,
            description: textareaVal
        };
        ajaxPost("/company/wxaddPosition", data, res => {
            if (res.code == 0) {
                showToast("text", "发布成功");
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 1500);
            }
        });
    },
    /**
     * 删除
     */
    delData() {
        let that = this
        wx.showModal({
            title: '提示',
            content: '确认删除本条求职岗位么？',
            success(res) {
                if (res.confirm) {
                    let data = {
                        token: wx.getStorageSync("token"),
                        position_id: that.data.id,
                    };
                    ajaxPost("/company/disablePosition", data, res => {
                        if (res.code == 0) {
                            showToast("text", "删除成功");
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1500);
                        }
                    });
                }
            }
        })
    },
    /**
     * 编辑
     */
    upData() {
        let {
            nameVal, //职业名字
            workVal, //职位类型
            region, //工作地点
            educationArr, //最低学历列表
            educationIndex, //最低学历key
            natureArr, //工作性质列表
            natureIndex, //工作性质key
            moneyVal, //薪资范围
            majorList, //专业需求列表
            textareaVal //职位描述
        } = this.data;

        if (!nameVal) {
            showToast("text", "请输入职业名字");
            return;
        }
        if (workVal.length == 0) {
            showToast("text", "请选择职位类型");
            return;
        }
        if (moneyVal.length == 0) {
            showToast("text", "请选择薪资范围");
            return;
        }
        if (!textareaVal) {
            showToast("text", "请职位描述");
            return;
        }
        let data = {
            token: wx.getStorageSync("token"),
            position_id: this.data.id,
            name: nameVal,
            type: workVal,
            salary: moneyVal,
            nature: natureArr[natureIndex],

            education: educationArr[educationIndex],

            province: region[0],
            city: region[1],
            district: region[2],
            major: majorList,
            description: textareaVal
        };
        ajaxPost("/company/wxEditPosition", data, res => {
            if (res.code == 0) {
                showToast("text", "保存成功");
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 1500);
            }
        });
    },
});